package socket.tcp;

import java.io.*;
import java.net.Socket;

/**
 * 客户端
 */
public class Client {
    public static void main(String[] args) {
        try {
            //1、创建客户端socket，指定服务器地址和端口
            Socket socket = new Socket("localhost", 8888);
            //2、获取输出流，向服务器发送信息
            OutputStream os = socket.getOutputStream();//字节输出流
            PrintWriter pw = new PrintWriter(os);//将字节流包装成打印流
            pw.write("用户名:soly;密码:123");
            pw.flush();
            //关闭输出流
            socket.shutdownOutput();
            //获取输入流
            //3、获取输入字节流
            InputStream is = socket.getInputStream();
            //4、获取字符流
            InputStreamReader isr = new InputStreamReader(is);
            //5、封装成缓冲流
            BufferedReader bf = new BufferedReader(isr);
            String data = null;
            while ((data = bf.readLine()) != null) {
                System.out.println("我是客户端，服务器说：" + data);
            }
            //关闭输入流
            socket.shutdownInput();
            //关闭资源
            bf.close();
            isr.close();
            is.close();
            pw.close();
            os.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
